package org.example.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.mapper.MysqlMapper;
import org.example.pojo.Mysql;
import org.example.pojo.MysqlPojo;
import org.example.util.Backup;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/mysql")
@Transactional
@Api("mysql测试")
public class MysqlController {

    @Autowired
    MysqlMapper mysqlMapper;

    @PostMapping(value = "/save")
    @ResponseBody
    @ApiOperation("存mysql文件")
    public void saveRes(HttpServletRequest request, @RequestBody MysqlPojo mysqlPojo){

        Backup a= new Backup();
        String url = a.save(request);
        mysqlPojo.setUrl(url);
        mysqlMapper.insertRes(mysqlPojo);
    }

    @PostMapping("/showSql")
    @ResponseBody
    @ApiOperation("显示所有权限")
    public ResponseData showUsers(int page, int limit){

        //使用PageHelper开启分页功能
        PageHelper.startPage(page,limit);

        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Mysql> roles = mysqlMapper.selectResId();


        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Mysql> rolePageInfo = new PageInfo<>(roles);



        ResponseData jsonData = ResponseData.getJsonData(roles,(int)rolePageInfo.getTotal());

        return jsonData;
    }

}
